/*
* 单选构件
* */

import 'package:center_beijing/api/component_api.dart';
import 'package:center_beijing/api/factory_api.dart';
import 'package:center_beijing/api/product_api.dart';
import 'package:center_beijing/common/QRScan.dart';
import 'package:center_beijing/common/custom_floatingAction_buttonLocation.dart';
import 'package:center_beijing/common/enums.dart';
import 'package:center_beijing/common/logutil.dart';
import 'package:center_beijing/common/notification.dart';
import 'package:center_beijing/common/styles.dart';
import 'package:center_beijing/common/utils.dart';
import 'package:center_beijing/config/global_config.dart';
import 'package:center_beijing/config/routeConfig.dart';
import 'package:center_beijing/models/selectItem.dart';
import 'package:center_beijing/widgets/pull_select_widget.dart';
import 'package:center_beijing/widgets/search_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_pickers/pickers.dart';

class QualitySelectComponent extends StatefulWidget {
  @override
  _QualitySelectComponentState createState() => _QualitySelectComponentState();
}

class _QualitySelectComponentState extends State<QualitySelectComponent> with SingleTickerProviderStateMixin {
  EasyRefreshController _controller = EasyRefreshController();
  TextEditingController _textInput = TextEditingController();
  var spotCheck = Get.arguments['spotCheck'];
  var selectedList = Get.arguments['selectList'];
  List dataSource = [];
  int page = 1;
  int pageSize = 100;
  bool firstLoad = true;

  List selectComps = [];

  @override
  void initState() {
    super.initState();
    this.getInfoList();
  }

  @override
  void dispose() {
    super.dispose();
  }

  getInfoList() async {
    var pass = {
      'page': page,
      'page-size': pageSize,
      'projectId': GlobalConfig.selectFactory.value,
      'state': '1,2',
      'processStatus': '2',
      'code': _textInput.text,
    };
    var result = await getCurrentFactoryComponent(pass);
    if (this.page == 1) {
      dataSource.clear();
    }
    firstLoad = false;
    if (result != null) {
      if (result['data'].length == 0) {
        _controller.finishLoad(success: true, noMore: true);
      } else {
        if(result['data'].length>0){
          for(var item in result['data']){
            for(var element in selectedList){
              if(element['id']==item['id']){
                item['selected'] = true;
                break;
              }
            }
          }
        }
        dataSource.addAll(result['data']);
      }
    }
    this.setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('选择构件'),
        ),
        body: Container(
          color: Styles.MainBackgroundColor,
          padding:
          EdgeInsets.fromLTRB(0, 0, 0, MediaQuery.of(context).padding.bottom),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                color: Colors.white,
                child: Row(
                  children: [
                    Expanded(
                      flex: 2,
                      child: Container(
                        margin: EdgeInsets.fromLTRB(Styles.spacing16,
                            Styles.spacing8, Styles.spacing16, Styles.spacing8),
                        child: SearchWidget(
                          textInput: _textInput,
                          placeholder: '请输入构件编号',
                          textSubmit: (value) {
                            page = 1;
                            this.getInfoList();
                          },
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              Expanded(
                  child: NotificationListener(
                    onNotification: (_) {
                      FocusScope.of(context).requestFocus(new FocusNode());
                      return false;
                    },
                    child: this.dataSource.length > 0
                        ? EasyRefresh.custom(
                        shrinkWrap: true,
                        firstRefresh: false,
                        controller: _controller,
                        header: null,
                        footer: ClassicalFooter(
                            loadingText: '加载中',
                            loadText: '上拉加载',
                            noMoreText: '没有更多数据',
                            loadedText: '加载完成',
                            infoText: '更新于 %T',
                            textColor: Styles.NoMoreColor,
                            showInfo: false),
                        emptyWidget: (dataSource.length == 0 && !firstLoad)
                            ? Center(
                          child: Image(
                            image: AssetImage('assets/images/no_data.png'),
                            width: 128,
                            height: 150,
                          ),
                        )
                            : null,

                        slivers: <Widget>[
                          SliverList(
                            delegate: SliverChildBuilderDelegate(
                                  (context, index) {
                                return GestureDetector(
                                  onTap: () async {
                                    var result = await Get.toNamed(RouteConfig.qualityFormEdit,
                                        arguments: {
                                          'compDetail':this.dataSource[index],
                                          'spotCheck':spotCheck
                                        });
                                    if(result!=null){
                                      Get.back(result: 1);
                                    }
                                  },
                                  child: Container(
                                    padding: EdgeInsets.fromLTRB(
                                        Styles.spacing16,
                                        Styles.spacing8,
                                        Styles.spacing16,
                                        Styles.spacing8),
                                    color: Colors.white,
                                    child:Row(
                                      children: [
                                        Expanded(
                                            child: Container(
                                              color: Colors.white,

                                              margin: EdgeInsets.fromLTRB(
                                                  Styles.spacing8,
                                                  Styles.spacing4,
                                                  Styles.spacing8,
                                                  Styles.spacing4),
                                              child: Row(
                                                children: [
                                                  Expanded(
                                                    child: Column(
                                                      crossAxisAlignment:
                                                      CrossAxisAlignment.start,
                                                      children: [
                                                        Row(
                                                          children: [
                                                            Expanded(
                                                                child: Column(
                                                                  crossAxisAlignment:
                                                                  CrossAxisAlignment
                                                                      .start,
                                                                  children: [
                                                                    Text(
                                                                      this.dataSource[index]
                                                                      [
                                                                      'code'] !=
                                                                          null
                                                                          ? this.dataSource[
                                                                      index]
                                                                      ['code']
                                                                          : '--',
                                                                      style: TextStyle(
                                                                          color: Styles
                                                                              .DetailColor,
                                                                          fontSize: Styles
                                                                              .Middle15Font),
                                                                    ),
                                                                    Text(
                                                                      this.dataSource[index]
                                                                      [
                                                                      'compKind'] !=
                                                                          null
                                                                          ? this.dataSource[
                                                                      index]
                                                                      [
                                                                      'compKind']
                                                                      [
                                                                      'compKindName']
                                                                          : '--',
                                                                      style: TextStyle(
                                                                          color: Styles
                                                                              .DetailColor,
                                                                          fontSize: Styles
                                                                              .Middle15Font),
                                                                    ),
                                                                  ],
                                                                )),
                                                            Row(
                                                              children: [
                                                                Container(
                                                                  color: Styles
                                                                      .BlueColor,
                                                                  margin: EdgeInsets
                                                                      .fromLTRB(
                                                                      Styles
                                                                          .spacing4,
                                                                      0,
                                                                      Styles
                                                                          .spacing4,
                                                                      0),
                                                                  padding: EdgeInsets.fromLTRB(
                                                                      Styles.spacing4,
                                                                      Styles.spacing4,
                                                                      Styles.spacing4,
                                                                      Styles
                                                                          .spacing4),
                                                                  child: Text(
                                                                    (this.dataSource[index]
                                                                    [
                                                                    'state'] !=
                                                                        null &&
                                                                        this.dataSource[index]
                                                                        [
                                                                        'state'] ==
                                                                            2)
                                                                        ? '维修中'
                                                                        : '正常',
                                                                    style: TextStyle(
                                                                        color: Colors
                                                                            .white,
                                                                        fontSize: Styles
                                                                            .SmallFont),
                                                                  ),
                                                                ),
                                                                Container(
                                                                  color: Styles
                                                                      .OrangeColor,
                                                                  margin: EdgeInsets
                                                                      .fromLTRB(
                                                                      Styles
                                                                          .spacing4,
                                                                      0,
                                                                      Styles
                                                                          .spacing4,
                                                                      0),
                                                                  padding: EdgeInsets.fromLTRB(
                                                                      Styles.spacing4,
                                                                      Styles.spacing4,
                                                                      Styles.spacing4,
                                                                      Styles
                                                                          .spacing4),
                                                                  child: Text(
                                                                    productType.getType(
                                                                        this.dataSource[
                                                                        index]
                                                                        [
                                                                        'processStatus']),
                                                                    style: TextStyle(
                                                                        color: Colors
                                                                            .white,
                                                                        fontSize: Styles
                                                                            .SmallFont),
                                                                  ),
                                                                )
                                                              ],
                                                            )
                                                          ],
                                                        ),
                                                        Text(
                                                          this.dataSource[index]
                                                          ['orderInfo'] !=
                                                              null
                                                              ? this.dataSource[index]
                                                          ['orderInfo']
                                                          ['orderName']
                                                              : '--',
                                                          style: TextStyle(
                                                              color: Styles
                                                                  .LeftTitleColor,
                                                              fontSize:
                                                              Styles.NormalFont),
                                                        ),
                                                      ],
                                                    ),
                                                  ),
                                                ],
                                              ),
                                            ))
                                      ],
                                    ),
                                  ),
                                );
                              },
                              childCount: dataSource.length,
                            ),
                          ),
                        ])
                        : firstLoad
                        ? Center(
                      child: CircularProgressIndicator(),
                    )
                        : Center(
                      child: Image(
                        image: AssetImage('assets/images/no_data.png'),
                        width: 128,
                        height: 150,
                      ),
                    ),
                  ))
            ],
          ),
        ),

    );
  }
}
